#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <stdlib.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::listContain(const QStringList &p1, const QStringList &p2)
{
    for(auto p : p2) {
        if(p1.contains(p))
            return true;
    }
    return false;
}

void MainWindow::on_pushButton_getList_clicked()
{
    FILE * fp = popen(ui->lineEdit_getList->text().toLocal8Bit(), "r");
    if(fp == NULL)
        return;
    char buffer[1024];
    int line = 0;
    QStringList list;
    QString content;
    while (fgets(buffer, sizeof(buffer), fp) != NULL)
        list.prepend(QByteArray(buffer).trimmed());
    pclose(fp);
    //
    logInfoList.clear();
    for(auto f : list) {
        LogInfo info;
        info.id = ++line;
        info.hash = f.split(' ').value(0);
        info.desc = f.mid(info.hash.length() + 1);
        logInfoList.append(info);
    }
    ui->plainTextEdit->setPlainText(LogInfo::toString(logInfoList));
}

void MainWindow::on_pushButton_getChangeFiles_clicked()
{
    QString r;
    for(LogInfo &l : logInfoList) {
        QProcess p;
        p.start(QString("git show --name-only --pretty=format: " + l.hash));
        p.waitForFinished();
        l.files = QString(p.readAll().trimmed()).split('\n');
    }
    ui->plainTextEdit->setPlainText(LogInfo::toString(logInfoList));
}

void MainWindow::on_pushButton_sort_clicked()
{
    QStringList filesSave;
    QString r;
    for(int k = 0; k < logInfoList.count(); k ++) {
        LogInfo &cell = logInfoList[k];
        if(k == 0) {
            filesSave = cell.files;
            continue;
        }
        if(listContain(filesSave, cell.files)) {
            filesSave.clear();
            logInfoList[k - 1].isBreak = true;
        }
        filesSave += cell.files;
    }
    ui->plainTextEdit->setPlainText(LogInfo::toString(logInfoList));
}

QString LogInfo::toString(const QList<LogInfo> &list0)
{
    QString suffix = "---------------------------------";
    QString first;
    QString r;
    QList<LogInfo> list = list0;
    if(list.count() > 0)
        list.last().isBreak = true;
    int pickCount = 0;
    for(LogInfo l : list) {
        pickCount ++;
        r +=  toString(l) + "\n";
        for(auto file : l.files) {
            r += "#" + file + "\n";
        }
        if(first.isEmpty() && !l.isBreak)
            first = l.hash;
        if(l.isBreak) {
            if(first.isEmpty()){
                r += suffix + QString("cmd:git cherry-pick %1, total:%3").arg(l.hash).arg(pickCount) + suffix + "\n";
            } else {
                r += suffix + QString("cmd:git cherry-pick %1^...%2, total:%3").arg(first).arg(l.hash).arg(pickCount) + suffix + "\n";
            }
            first = "";
            pickCount = 0;
        }
    }
    return r;
}

QString LogInfo::toString(const LogInfo &l)
{
    return QString("%1.%2 %3").arg(l.id).arg(l.hash).arg(l.desc);
}
